<template xmlns="http://www.w3.org/1999/html">
  <el-row>
    <el-col :span="4">
      <sea-request url="/sys/dept/list" :result-parser="resultParser">
        <el-row slot-scope="{response, reload, loading}"
                class="box with-border with-padding"
                style="min-height: 450px;">
          <el-col class="sea-card">
            <el-form>
              <el-input placeholder="请输入内容" v-model="filterText"
                        class="input-with-select" style="margin-bottom: 12px;">
              </el-input>
            </el-form>

            <el-button type="text" @click="reload" class="pull-right" title="刷新用户分组信息">
              <i class="fa fa-refresh"></i> 刷新
            </el-button>
          </el-col>
          <el-col>
            <sea-skeleton :data="response" :loading="loading">
              <el-tree ref="tree" :data="response"
                       @node-click="handleNodeClick" :filter-node-method="filterNode"
                       default-expand-all :expand-on-click-node="false">
              </el-tree>
            </sea-skeleton>
          </el-col>
        </el-row>
      </sea-request>
    </el-col>


    <el-col :span="20">
      <sea-data-table ref="tableObject" url="/sys/user/page">
        <!--表头-->
        <sea-table-header slot="header">
          <!--标题栏-->
          <template slot="title">
            <sea-table-title title="用户管理" subtitle=""></sea-table-title>
          </template>

          <!--按钮栏-->
          <template slot="buttons">
            <el-button plain size="small" title="新增" icon="fa fa-plus" @click="onAction('new')"></el-button>
          </template>
        </sea-table-header>

        <!-- 弹窗、抽屉 -->
        <template slot="drawer">
          <sea-dialog ref="create_dialog">
            <create ref="create" :on-succeed="onSucceed"></create>
          </sea-dialog>
          <sea-dialog ref="modify_dialog">
            <modify ref="modify" :on-succeed="onSucceed"></modify>
          </sea-dialog>
        </template>

        <!--表头-->
        <template slot="columns">
          <el-table-column property="account" label="账号" width="180"></el-table-column>
          <el-table-column property="username" label="昵称" width="180"></el-table-column>
          <el-table-column property="phone" label="电话" width="180"></el-table-column>
          <sea-col-dictionary type="dic" property="gender" label="性别" width="96"
                                 options="non.user_gender"></sea-col-dictionary>
          <sea-col-switch property="disabled" label="是否可用" width="96" :change="onAction"
                             :reverse="true"></sea-col-switch>
          <el-table-column property="gmtCreate" label="创建时间" width="180"></el-table-column>
          <el-table-column property="gmtModified" label="修改日期" width="180"></el-table-column>

          <!--操作栏-->
          <sea-table-actionbar width="180" :on-action="onAction" :buttons="buttons"></sea-table-actionbar>
        </template>
      </sea-data-table>
    </el-col>
  </el-row>
</template>

<script>
import create from "./create";
import modify from "./modify";
import Bamboo from "@/lang/bamboo";

export default {
    components: {create, modify},
    data() {
        return {
            // 操作名称
            operation: undefined
            // 当前操作的行级对象
            , row_data: undefined
            // 操作栏
            , buttons: [
                {type: 'btn', evt: 'edit', label: '编辑', title: '编辑当前行'},
                {type: 'btn', evt: 'reset', label: '重置密码', title: '重置用户密码'}
            ]
            // 模糊搜索组名
            , filterText: undefined
            // 选中的部门
            , selectDept: undefined
            // end--
        }
    },
    watch: {
        filterText(val) {
            this.$refs.tree.filter(val);
        }
    },
    methods: {
        /**
         * 查询结果转换为树形结构
         */
        resultParser: function (list) {
            if (this.$layers.isNotEmpty(list)) {
                this.row_data = list[0];
            }
            console.log(list);
            let arr = this.$bamboo.wrapList(list, Bamboo.getNameOption());
            return this.$bamboo.toTree(arr);
        },

        /**
         * 部门点击事件
         */
        handleNodeClick: function (data) {
            this.selectDept = data;
        },

        /**
         * 模糊检索部门
         */
        filterNode(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        /**
         * 操作成功回调
         */
        onSucceed: function () {
            let evt = this.operation;
            if (evt === 'new') {
                this.$refs.create_dialog.close();
                this.$refs.tableObject.reload();
            } else if (evt === 'edit') {
                this.$refs.modify_dialog.close();
                this.$refs.tableObject.reload();
            }
        },
        /**
         * 操作栏事件分发
         * @param evt 事件
         * @param row 行级对象
         * @param old 可供回滚的参数
         */
        onAction: function (evt, row, old) {
            this.operation = evt;
            if (evt === 'new') {
                let dialog = {title: `新建`, width: '50%'};
                this.$refs.create_dialog.open(dialog);
            } else if (evt === 'edit') {
                let dialog = {title: `更新`, width: '70%'};
                this.$refs.modify_dialog.open(dialog).then(() => {
                    this.row_data = row;
                });
            }
        }
    }
}
</script>
